﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using AMS.Inventory.DataAccess;
using AMS.Inventory.Entities;
using AMS.Inventory.Importors;
using VitCode.Castle.Helper;

namespace AMSInventoryWeb.BaseData.Importors
{
    public class SelectAssetImportor : AbstractImportor
    {
        private const string KEY_COL_NAME = "资产条码";
        private CoreDataAccess m_Access;

        private StringCollection m_keyInfos = new StringCollection();
        private NameObjectDictionary m_ObjectSets = new NameObjectDictionary();

        private string m_PlanId;

        public int MaxAssetAmount
        { get; set; }

        public SelectAssetImportor(string path, string sheetName, string planId)
        {
            m_PlanId = planId;
            m_ErrorMessage = new ErrorResult();
            m_ErrorMessage.TableName = "出错信息";
            FilePath = path;
            SheetName = sheetName;
            m_IsTrans = true;
            MaxAssetAmount = 1000;
        }

        private StringBuilder m_SaveErrMsg;

        public StringBuilder SaveErrMsg
        {
            get;
            set;
        }

        private static string ObjectName
        {
            get { return "待盘资产"; }
        }

        public string CurrPlanId
        {
            get { return m_PlanId; }
        }

        private CoreDataAccess DataAccess
        {
            get
            {
                if (m_Access == null)
                    m_Access = new CoreDataAccess();
                return m_Access;
            }
        }

        protected override string[] Titles
        {
            get { return new[] {KEY_COL_NAME}; }
        }

        protected override object DataRowToObject(DataRow row)
        {
            string strKeyVal = ReturnColumnValue(row, KEY_COL_NAME);
            var result = new InventoryInfo();
            result.PlanId = new Guid(m_PlanId);
            var li = ((LedgerInfo) m_ObjectSets[strKeyVal]);
            result.SetProByLedgerInfo(li);
            UpdateCount++;
            return result;
        }

        protected override void SaveObjects(IList objects)
        {
            if (IsTrans)
                DataAccess.BeginTransaction();
            try
            {
                DataAccess.Session.Delete("From "+typeof(InventoryInfo).Name+" o where o.PlanId='" + m_PlanId + "'");
                ArrayList alAssetInfos = new ArrayList();
                foreach (InventoryInfo obj in objects)
                {
                    alAssetInfos.Add(obj);
                }
                m_SaveErrMsg = new StringBuilder();
                DataAccess.SavePlan(new Guid(m_PlanId), alAssetInfos, MaxAssetAmount, ref m_SaveErrMsg);
                if (IsTrans && m_SaveErrMsg.Length==0)
                    DataAccess.Commit();
            }
            catch (Exception e)
            {
                if (m_IsTrans)
                    DataAccess.RollBack();
                throw e;
            }
            finally
            {
                if (m_IsTrans)
                    DataAccess.CloseSession();
            }
        }

        protected override void GetDBRecordSet()
        {
            try
            {
                m_ObjectSets = DataAccess.GetObjectsByKeyValues(typeof (LedgerInfo).Name, "Barcode", m_keyInfos);
            }
            finally
            {
                DataAccess.CloseSession();
            }
        }

        protected override ExcelHelper CreateExcelHelper(string path, string sheetName)
        {
            var helper = new ExcelHelper(path, sheetName);
            helper.NotRepleateColumns.AddColumnByName(KEY_COL_NAME);
            return helper;
        }


        protected override void CreateDBSearchCondition(DataRow row, ErrorResult errorResult, int excelRowNumber)
        {
            string Message = "";
            string strObjKeyVal = ReturnColumnValue(row, KEY_COL_NAME);

            if (!CheckFieldLength(row, KEY_COL_NAME, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            else if (!m_keyInfos.Contains(strObjKeyVal))
                m_keyInfos.Add(strObjKeyVal);
        }

        protected override void CheckInDb(DataRow row, int excelRowNumber, ErrorResult errorResult)
        {
            string msg = "";
            string strObjKeyProVal = ReturnColumnValue(row, KEY_COL_NAME);

            if (!CheckChildObjectExists(m_ObjectSets, strObjKeyProVal, KEY_COL_NAME, out msg))
                errorResult.SetMessage(excelRowNumber,
                                       KEY_COL_NAME + "为: [ " + strObjKeyProVal + " ] 的" + ObjectName +
                                       "不存在计划为待盘资产");
        }
    }
}